import json
import subprocess
import time

from django.http import HttpResponse
from django.shortcuts import redirect,render
from django.views.decorators.csrf import csrf_exempt

def main(request):
    return redirect('/static/index.html')

def myvar(request, myname):
    return HttpResponse('my name is {0}'.format(myname))

def myre(request):
    return HttpResponse('sjg re re re!')

def ajax(request):
    time.sleep(3)
    return HttpResponse('sjg ajax!')

def ajaxget(request):
    username = request.GET.get('username')
    password = request.GET.get('password')
    return HttpResponse('ajax get {0} {1}!'.format(username, password))

@csrf_exempt
def ajaxpost(request):
    info = request.body
    print(info)
    info = json.loads(info)
    print(info)
    return HttpResponse('json loads password: ' + info.get('password'))
@csrf_exempt
def template(request):
    if not request.POST:
        return render(request, 'tplhello.html')
    username = request.POST.get('username')
    password = request.POST.get('password')
    if username == 'sjg' and password == 'sjgpwd':
        return redirect('/static/index.html')
    return render(request, 'tplhello.html', {'result':'user password is wrong.!'})

def get(request):
    print(request.GET)
    username = request.GET.get('username')
    password = request.GET.get('password')
    return HttpResponse(username + password)

@csrf_exempt
def post(request):
    print(request.POST)
    username = request.POST.get('username')
    password = request.POST.get('password')
    return HttpResponse(username + password)